﻿using System;
using System.Collections;
using System.Collections.Generic;
using CalcSharp.Core.Containers;
using CalcSharp.Core.Plugin;
using plug_stringutils.Properties;

namespace plug_stringutils
{
    public class MainClass: ICMDPlugin
    {
        private IPluginHost host;

        public Tresult Run(ArrayList parameters)
        {
            Tresult ret = new Tresult();

            string[] pars = new string[parameters.Count];
            for (int i = 0; i < pars.Length; i++)
            {
                pars[i] = Convert.ToString(parameters[i]);
                if (pars[i].StartsWith("~")) pars[i] = host.String_mem[pars[i]];
            }

            switch (host.CmdArray[0].ToUpper())
            {
                case "STR_LEN":
                    ret.floatingpt = pars[0].Length;
                    break;
                case "STR_APP":
                    ret.stringtype = StrUtils.Str_app(pars);
                    break;
                case "STR_CMP":
                    ret.floatingpt = StrUtils.Str_cmp(pars[0], pars[1]);
                    break;
                case "STR_CCMP":
                    ret.floatingpt = StrUtils.Str_ccmp(pars[0], pars[1]);
                    break;
                case "STR_OCC":
                    ret.floatingpt = StrUtils.Str_occ(pars[0], pars[1]);
                    break;
                case "STR_UP":
                    ret.stringtype = pars[0].ToUpper();
                    break;
                case "STR_DOWN":
                    ret.stringtype = pars[0].ToLower();
                    break;
                case "STR_REPL":
                    ret.stringtype = pars[0].Replace(pars[1], pars[2]);
                    break;
                case "STR_REGEX":
                    ret.stringtype = StrUtils.Str_regex(pars[0], pars[1], pars[2]);
                    break;
                case "STR_IOF":
                    ret.floatingpt = pars[0].IndexOf(pars[1]);
                    break;
                case "STR_CHR":
                    ret.stringtype = StrUtils.Str_chr(pars[0], Convert.ToInt32(pars[1]));
                    break;
                case "STR_SUBSTR":
                    ret.stringtype = StrUtils.Str_substr(pars[0], Convert.ToInt32(pars[1]), Convert.ToInt32(pars[2]));
                    break;
                case "STR_HASH":
                    ret.floatingpt = StrUtils.Str_hash(pars[0]);
                    break;
                case "STR_MD5":
                    ret.stringtype = StrUtils.Str_MD5(pars[0]);
                    break;
                case "STR_SHA1":
                    ret.stringtype = StrUtils.Str_SHA1(pars[0]);
                    break;
            }
            return ret;
        }

        public string Name
        {
            get 
            {
                return "STR_LEN;STR_APP;STR_CMP;STR_CCMP;STR_OCC;STR_UP;STR_DOWN;" +
                       "STR_REPL;STR_REGEX;STR_IOF;STR_CHR;STR_SUBSTR;STR_HASH;"+
                       "STR_MD5;STR_SHA1";
            }
        }

        public Dictionary<string, string> autocomplete
        {
            get { return null; }
        }

        public string Help
        {
            get 
            {
                string swit;
                if (host.CmdArray.Length > 1) swit = host.CmdArray[1].ToUpper();
                else swit = host.CmdArray[0].ToUpper();
                switch (swit)
                {
                    case "STR_LEN":
                        return Resources.str_len;
                    case "STR_APP":
                        return Resources.str_app;
                    case "STR_CMP":
                        return Resources.str_cmp;
                    case "STR_CCMP":
                        return Resources.str_ccmp;
                    case "STR_OCC":
                        return Resources.str_occ;
                    case "STR_UP":
                        return Resources.str_up;
                    case "STR_DOWN":
                        return Resources.str_down;
                    case "STR_REPL":
                        return Resources.str_repl;
                    case "STR_REGEX":
                        return Resources.str_regex;
                    case "STR_IOF":
                        return Resources.str_iof;
                    case "STR_CHR":
                        return Resources.str_chr;
                    case "STR_SUBSTR":
                        return Resources.str_substr;
                    case "STR_HASH":
                        return Resources.str_hash;
                    case "STR_MD5":
                        return Resources.str_md5;
                    case "STR_SHA1":
                        return Resources.str_sha1;
                    default:
                        return "";
                }
            }
        }

        public IPluginHost Host
        {
            get
            {
                return host;
            }
            set
            {
                host = value;
                host.Register(this);
            }
        }
    }
}
